import {takeEvery, put, delay} from 'redux-saga/effects'
import request from "../../utils/request";
import {AUTH} from '../../redux/actions/types'
import {Toast} from 'antd-mobile'
import {Router} from 'react-router-dom'

function* login() {
    yield takeEvery(AUTH.login, function* (data) {

        //data {type: "auth_login", payload: {}}
        const {payload, onSuccess} = data;
        Toast.loading("请求中...", 0);
        const response = yield request('/auth/login', {
            method: 'POST',
            data: payload,
        });
        Toast.hide();
        if (response.code === 200) {
            sessionStorage.setItem('token', response.token);
            Toast.success('登录成功!', 1.5, function () {
                onSuccess()
            });
        } else {
            Toast.offline('用户名密码不能为空');
        }
    });
}

export default {login}
